package com.jie.demo.mock;

import org.junit.Test;

import java.util.LinkedList;
import java.util.List;

import static org.mockito.Mockito.*;

/**
 * spy 修饰的是真实的对象，不是mock的对象，也可以对对象进行打桩等
 */
public class SpyMock {

    @Test
    public void test() {
        List list = new LinkedList();
        List spy = spy(list);

        //optionally, you can stub out some methods:
        // 你可以为某些函数打桩
        when(spy.size()).thenReturn(100);

        //using the spy calls *real* methods
        // 通过spy对象调用真实对象的函数
        spy.add("one");
        spy.add("two");

        //prints "one" - the first element of a list
        // 输出第一个元素
        System.out.println(spy.get(0));

        //size() method was stubbed - 100 is printed
        // 因为size()函数被打桩了,因此这里返回的是100
        System.out.println(spy.size());

        //optionally, you can verify
        // 交互验证
        verify(spy).add("one");
        verify(spy).add("two");
    }
}
